package com.huitone.gddw.service.impl.worktable;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.SvrSpeed;
import com.huitone.gddw.dao.worktable.paramconfigmanage.ISvrSpeedDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.ISvrSpeedService;
import com.huitone.gddw.service.dto.SvrSpeedDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 短接接入分级接口
 * @author Weiliyu
 *
 */
@Service
public class SvrSpeedService implements ISvrSpeedService {

	@Autowired
	private ISvrSpeedDao svrSpeedDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;
	
	@Override
	public QueryPage<SvrSpeedDTO> selectPage(QueryPage<SvrSpeed> page) {
		Map<String, Object> map = new HashMap<>();
		map.put("svrNode", page.getParams().get("svrNode"));
		map.put("serviceCode", page.getParams().get("serviceCode"));
		map.put("limittype", page.getParams().get("limittype"));
		map.put("startDate", page.getParams().get("startDate"));
		map.put("endDate", page.getParams().get("endDate"));
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<SvrSpeedDTO> result = svrSpeedDao.selectPage(map);
		int totalCount = svrSpeedDao.selectCount(map);
		
		QueryPage<SvrSpeedDTO> queryPage = new QueryPage<>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public SvrSpeed selectByPrimaryKey(String recId) {
		return svrSpeedDao.selectByPrimaryKey(recId);
	}

	@Override
	public SvrSpeed toUpdate(String recId) {
		return this.selectByPrimaryKey(recId);
	}

	@Override
	public void save(SvrSpeed entity) {
		entity.setStatus("1");	//默认有效
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		svrSpeedDao.insertSelective(entity);
		
		this.updateParamRec("保存短信接入分级信息");
	}

	@Override
	public void update(SvrSpeed entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		svrSpeedDao.updateByPrimaryKey(entity);
		
		this.updateParamRec("更新短信接入分级信息");
	}

	@Override
	public void delete(String recId) {
		svrSpeedDao.deleteByPrimaryKey(recId);
		this.updateParamRec("删除短信接入分级信息");
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.SM_LEVEL, desc);
	}

	@Override
	public List<SvrSpeed> selectBySvr(String svrNode) {
		return svrSpeedDao.selectBySvr(svrNode);
	}

	@Override
	public void deleteBySvr(String svrNode) {
		svrSpeedDao.deleteBySvr(svrNode);
	}

}
